package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr;

import com.iwhalecloud.bss.kite.client.api.scene.operate.IOperateSceneCommonService;
import com.iwhalecloud.bss.kite.client.api.scene.query.IQuerySceneService;
import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.inst.IProdInstService;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalServiceOfferConsts;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.iwhalecloud.bss.kite.web.context.KiteWebContext;
import com.iwhalecloud.bss.kite.web.service.accept.attr.handler.AbstractAttrHandler;
import com.ztesoft.bss.common.util.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class OpenWayGJHandler extends AbstractAttrHandler {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    public IProdInstService prodInstService;

    @Autowired
    private IOperateSceneCommonService operateSceneCommonService;

    @Override
    public void init(CommonAttrVO commonAttr) {
        IQuerySceneService querySceneService = SpringUtil.getBean(IQuerySceneService.class);
        String sceneInstId = KiteWebContext.getContext().getCurrentSceneInstId();
        KiteSceneInst sceneInst = querySceneService.getSceneInst(sceneInstId);
        String serviceOfferId = sceneInst.getServiceOfferId();
        String nodeCode = querySceneInstService.getExtParameterValue(sceneInstId, "nodeCode");

        // 签订、追单、业务开通-重提  不允许修改开通方式
        if (KiteStringUtils.equalsAny(nodeCode, "OrderSigned","OrderResubmit") || LocalServiceOfferConsts.SERVICE_OFFER_FOLLOW.equals(serviceOfferId)) {
            commonAttr.setIsEdit("F");
            setCommonAttr(commonAttr);
        }
    }

    @Override
    public DataResult<Boolean> apply(String sceneInstId, String fieldName, String value, String valueDesc) {
        // 无需核查原因
        KiteAttr reasonAttr = querySceneInstService.getCommonAttrsByFieldName(sceneInstId, "ORD10015", "93");
        if (Objects.nonNull(reasonAttr)) {
            // 当开通方式选择“无需核查直开”的时候显示 无需核查原因
            if (KiteStringUtils.isNotEmpty(value)) {
                if (KiteStringUtils.equals("0", value)) {
                    reasonAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
                    operateSceneCommonService.setCommonAttr(sceneInstId, reasonAttr);
                } else {
                    reasonAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
                    operateSceneCommonService.setCommonAttr(sceneInstId, reasonAttr);
                }
            }
        }

        return super.apply(sceneInstId, fieldName, value, valueDesc);
    }
}
